package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.service.IUserInfoRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Reference
    private IUserInfoRedisService userInfoRedisService;



    @Override
    public UserInfo get(long id) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        System.out.println(userInfo);
        return userInfo;
    }

    @Override
    public boolean selectCountPhone(String phone) {
        return userInfoMapper.selectCountPhone(phone)>0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //生成验证码
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        //将生成的验证码缓存在redis中
       // redisTemplate.opsForValue().setPhone(phone,verifyCode, Consts.USER_INFO_TOKEN_VAI_TIME, TimeUnit.MINUTES);
        userInfoRedisService.setPhone(phone,verifyCode);
        //整合验证码
        StringBuffer st= new StringBuffer(100);
        st.append("你的验证码是").append(verifyCode).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内使用");
        System.out.println(st);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判断参数不能为空
        AssertUtil.hasLength(phone, "手机号不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        //判断2次密码是不是一样的
        AssertUtil.isEquals(password, rpassword, "2次密码不一致");

        //判断验证码是不是相等
        String code = userInfoRedisService.getPhone(phone);
        if (!verifyCode.equalsIgnoreCase(code)) {
            throw new LogicException("验证码错误");
        }

        //注册
        UserInfo userIofo = new UserInfo();
        userIofo.setNickname(nickname);
        userIofo.setLevel(1);
        Md5Hash md5Hash = new Md5Hash(password, phone);
        userIofo.setPassword(md5Hash.toString());
        userIofo.setPhone(phone);
        userIofo.setState(UserInfo.STATE_NORMAL);
        userIofo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userIofo);
    }

    @Override
    public String login(String username, String password) {
        //验证参数的正确性
        AssertUtil.hasLength(username,"账号不能为空");
        AssertUtil.hasLength(password,"密码不能为空");

        //验证码账户密码是否正确
        String encryptPasswod = new Md5Hash(password, username).toString();
        UserInfo userInfo = userInfoMapper.checkLogin(username, encryptPasswod);
        if (userInfo == null) {
            throw new LogicException("账号或者密码不正确");
        }
        //生成token
        String token = UUID.randomUUID().toString().replace("-", "");
        userInfoRedisService.addToken(token, userInfo);
        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }


}
